//
//  MusicDetailViewController.m
//  XMLY
//
//  Created by lanou on 15/11/5.
//  Copyright © 2015年 曾杰. All rights reserved.
//

#import "MusicDetailViewController.h"
#import "PKRequestManager.h"
#import "MusicModel.h"
#import "MusicDetailTableViewCell.h"
#import "playListView.h"
#import "UIImageView+WebCache.h"
#import "ChildListModel.h"
#import "MusicPlayViewController.h"
@interface MusicDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)NSMutableArray * dataArr;
@property  (nonatomic, strong)UITableView * detailTableView;
@property (nonatomic, strong)UIButton * backButton;
@property (nonatomic, strong)playListView * listView;
@property (nonatomic, strong)MusicModel * headModel;
@end

@implementation MusicDetailViewController

-(NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        self.dataArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArr;
}
-(void)requestData
{
    NSString * urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/20?albumId=%@&isAsc=true&pageSize=20",self.albumId,self.albumId];
    
    NSLog(@"%@",self.albumId);
[PKRequestManager requestWithType:GET urlString:urlStr parDic:nil finish:^(NSData *data) {
    NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
    NSLog(@"%@",dic);
    NSDictionary * dict = [dic objectForKey:@"tracks"];
    NSDictionary * headDic = [dic objectForKey:@"album"];
    
    _headModel = [[MusicModel alloc]init];
    [_headModel setValuesForKeysWithDictionary:headDic];
    [_listView.singerView sd_setImageWithURL:[NSURL URLWithString:_headModel.avatarPath]];
    [_listView.titleView sd_setImageWithURL:[NSURL URLWithString:_headModel.coverMiddle]];
    _listView.singerLabel.text = _headModel.nickname;
    _listView.introLabel.text = _headModel.intro;
    _listView.nameLabel.text = _headModel.title;
    NSArray * tags = [_headModel.tags componentsSeparatedByString:@","];
       for (int i = 0; i<tags.count; i++) {
       
        UIButton * button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.frame = CGRectMake(SCREEN_WIDTH/4+40 +50*i,130 , 40, 20);
        
        button.layer.cornerRadius =10;
        button.layer.masksToBounds = YES;
        [button setTitle:tags[i] forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateNormal)];
        button.titleLabel.font = [UIFont systemFontOfSize:11];
        button.layer.borderWidth = 0.5;
        button.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        [_listView.backGroundView addSubview:button];
    }
    
    [_listView.backButton addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    
    NSArray * list = [dict objectForKey:@"list"];
    for (NSDictionary * dictt  in list) {
        ChildListModel * model = [[ChildListModel alloc]init];
        [model setValuesForKeysWithDictionary:dictt];
        [self.dataArr addObject:model];
    }
    [self.detailTableView reloadData];
} error:^(NSError *error) {
    NSLog(@"%@",error);
}];
    
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestData];
    self.view.backgroundColor = [UIColor whiteColor];
   
    _detailTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height) style:(UITableViewStylePlain)];
    _detailTableView.delegate = self;
    _detailTableView.dataSource = self;
    [_detailTableView registerClass:[MusicDetailTableViewCell class] forCellReuseIdentifier:@"cell"];
    _detailTableView.rowHeight = 70;
    [self.view addSubview:_detailTableView];

   
    _listView = [[playListView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/2-90)];
    _detailTableView.tableHeaderView = _listView;
    

}

-(void)back

{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    MusicDetailTableViewCell * cell = [self.detailTableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    ChildListModel * model = self.dataArr[indexPath.row];
    [cell configCellWith:model];
    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    MusicPlayViewController * playVC = [[MusicPlayViewController alloc]init];
    playVC.musicArr = self.dataArr;
    playVC.index = indexPath.row;
    [self presentViewController:playVC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
